package edu.service.impl;

import edu.service.RolePermissionService;
import edu.vo.RolePermission;
import edu.vo.ID.RolePermissionId;
import edu.repository.RolePermissionRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class RolePermissionServiceImpl implements RolePermissionService {

    private final RolePermissionRepository rolePermissionRepository;

    public RolePermissionServiceImpl(RolePermissionRepository rolePermissionRepository) {
        this.rolePermissionRepository = rolePermissionRepository;
    }

    @Override
    public RolePermission saveRolePermission(RolePermission rolePermission) {
        return rolePermissionRepository.save(rolePermission);
    }

    @Override
    public Optional<RolePermission> getRolePermissionById(RolePermissionId id) {
        return rolePermissionRepository.findById(id);
    }

    @Override
    public List<RolePermission> getAllRolePermissions() {
        return rolePermissionRepository.findAll();
    }

    @Override
    public void deleteRolePermissionById(RolePermissionId id) {
        rolePermissionRepository.deleteById(id);
    }

    @Override
    public RolePermission updateRolePermission(RolePermissionId id, RolePermission rolePermission) {
        return rolePermissionRepository.findById(id)
                .map(existingRolePermission -> {
                    existingRolePermission.setRole(rolePermission.getRole());
                    existingRolePermission.setPermission(rolePermission.getPermission());
                    return rolePermissionRepository.save(existingRolePermission);
                })
                .orElse(null);
    }
}